package com.example.pulllistview;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private RefeshListView rflist;
    private List<String> mList=new ArrayList<>();
    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rflist= (RefeshListView) findViewById(R.id.rflist);
        initData();
        adapter=new MyAdapter();
        rflist.setAdapter(adapter);
        //设置一个刷新监听
        rflist.setOnRefreshOnlistener(new RefeshListView.OnRefreshListener() {
            @Override
            public void onPullRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mList.add(0,"我是新数据");
                        adapter.notifyDataSetChanged();
                        //隐藏头布局
                        rflist.onRefreshFinish();
                    }
                }, 3000);
            }
        });
        }

    /**
     * 初始化数据
     */
    private void initData() {
        for (int i=0;i<50;i++) {
            mList.add("我是条目："+i);
        }
    }


    private class MyAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv=new TextView(MainActivity.this);
            tv.setText(mList.get(position));
            tv.setPadding(20,5,0,5);
            return tv;
        }
    }
}
